package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.enums.DeleteEnum;
import com.member.common.enums.EnableStatusEnum;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizCarousel;
import com.member.shop.domain.SysBizGoods;
import com.member.shop.domain.SysBizPop;
import com.member.shop.domain.SysBizShopCategory;
import com.member.shop.service.ISysBizGoodsService;
import com.member.shop.service.ISysBizShopCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【商品分类】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-12-28
 **/
@RestController
@RequestMapping("/shop/category")
public class SysBizShopCategoryController extends BaseController {
    @Resource
    private ISysBizShopCategoryService sysBizShopCategoryService;
    @Resource
    private ISysBizGoodsService sysBizGoodsService;

    private void setTenantId(SysBizShopCategory sysBizShopCategory) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizShopCategory.setTenantId(tenantId);
        }
    }

    /**
     * 查询商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('shop:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizShopCategory sysBizShopCategory) {
        sysBizShopCategory.setDeleted(Long.valueOf(DeleteEnum.NOT_DELETE.getKey()));
        setTenantId(sysBizShopCategory);
        List<SysBizShopCategory> list = sysBizShopCategoryService.selectSysBizShopCategoryList(sysBizShopCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('shop:category:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizShopCategory sysBizShopCategory) {
        setTenantId(sysBizShopCategory);
        List<SysBizShopCategory> list = sysBizShopCategoryService.selectSysBizShopCategoryList(sysBizShopCategory);
        ExcelUtil<SysBizShopCategory> util = new ExcelUtil<SysBizShopCategory>(SysBizShopCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizShopCategoryService.selectSysBizShopCategoryById(id));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('shop:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizShopCategory sysBizShopCategory) {
//        if (sysBizShopCategory.getParentId() != null) {
//            List<SysBizGoods> list = sysBizGoodsService.list(new LambdaQueryWrapper<SysBizGoods>()
//                    .eq(SysBizGoods::getCategoryId, sysBizShopCategory.getParentId())
//                    .eq(SysBizGoods::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey()));
//            if (!list.isEmpty()) {
//                return warn("该分类下有商品，不能添加子分类");
//            }
//        }
        LambdaQueryWrapper<SysBizShopCategory> sysBizShopCategoryLambdaQueryWrapper = new LambdaQueryWrapper<SysBizShopCategory>();
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getCategoryName, sysBizShopCategory.getCategoryName());
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getParentId, sysBizShopCategory.getParentId());
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getTenantId, getTenantId());
        List<SysBizShopCategory> sysBizShopCategories = sysBizShopCategoryService.selectSysBizShopCategoryList(sysBizShopCategoryLambdaQueryWrapper);
        if (!sysBizShopCategories.isEmpty()) {
            return warn("该分类名称已存在");
        }
        return toAjax(sysBizShopCategoryService.insertSysBizShopCategory(sysBizShopCategory));
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('shop:category:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizShopCategory sysBizShopCategory) {
        LambdaQueryWrapper<SysBizShopCategory> sysBizShopCategoryLambdaQueryWrapper = new LambdaQueryWrapper<SysBizShopCategory>();
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getCategoryName, sysBizShopCategory.getCategoryName());
        sysBizShopCategoryLambdaQueryWrapper.ne(SysBizShopCategory::getId, sysBizShopCategory.getId());
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getParentId, sysBizShopCategory.getParentId());
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getTenantId, getTenantId());
        List<SysBizShopCategory> sysBizShopCategories = sysBizShopCategoryService.selectSysBizShopCategoryList(sysBizShopCategoryLambdaQueryWrapper);
        if (!sysBizShopCategories.isEmpty()) {
            return warn("该分类名称已存在");
        }
        return toAjax(sysBizShopCategoryService.updateSysBizShopCategory(sysBizShopCategory));
    }

    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('shop:category:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        if (sysBizShopCategoryService.hasChildById(id)) {
            return warn("存在子目录,不允许删除");
        }
        //判断该分类下是否有商品
        LambdaQueryWrapper<SysBizGoods> sysBizGoodsLambdaQueryWrapper = new LambdaQueryWrapper<SysBizGoods>();
        sysBizGoodsLambdaQueryWrapper.eq(SysBizGoods::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        sysBizGoodsLambdaQueryWrapper.eq(SysBizGoods::getCategoryTwoId, id);
        List<SysBizGoods> sysBizGoods = sysBizGoodsService.selectSysBizGoodsList(sysBizGoodsLambdaQueryWrapper);
        if (!sysBizGoods.isEmpty()) {
            return warn("该分类下级有商品绑定，请解绑后删除");
        }
        return toAjax(sysBizShopCategoryService.deleteSysBizShopCategoryById(id));
    }
}
